<?

switch($act){
	case 'detail':
		ShowDetail(isset($_GET['id'])?$_GET['id']:0);
		$tpl = 'detail';
		break;
	case 'search':
		Search();
		$title_page = RESULT_SEARCH;
		$tpl = 'list';
		break;
		
	case 'favourite':
		Favourite();
		$title_page = FAVOURITE_LIST;
		$tpl = 'list';
		break;
	case 'addsm':
		EditSM();
		break;	
	case 'payment':
		Payment();
		$tpl = 'payment';
		$title_page = 'Hướng dẫn thanh toán';
		break;
	case 'paymented':
		Paymented();
		//$tpl = 'payment';
		break;
	case 'atm':
		$tpl = 'atm';
		break;			
	case 'add':
		ShowList();
		$tpl='edit';
		$title_page = 'Thông tin đặt phòng khách sạn';
		break;	
	default:
		ShowList();
		$tpl='edit';
		break;
}
function Paymented()
{
	$type = $_GET['type'];
	$urlReturn = 'http://www.nhuquynh.com.vn';
	$cost = ($_SESSION['cost']);
	$emailRecevier = 'lieuminhtoan@gmail.com';
	//$emailRecevier = 'kamnhattuan@gmail.com';
	$orderCode = $_SESSION['code'];
	
	if($type == 'nganluong')
	{
		$url = 'https://www.nganluong.vn/button_payment.php?receiver='.$emailRecevier.'&product_name='.$orderCode.'&price='.$cost.'&return_url='.$urlReturn.'&comments='.urlencode("Thanh toán đơn hàng đặt phòng mã đơn hàng: [".$orderCode."]");
		$arr['paymentid'] = 2;
	}
	if($type == 'baokim')
	{
		$url = 'https://www.baokim.vn/payment/customize_payment/product?business='.$emailRecevier.'&product_name='.$orderCode.'&product_price='.$cost.'&product_quantity=1&total_amount='.$cost.'&product_description='.urlencode("Thanh toán đơn hàng đặt phòng mã đơn hàng: [".$orderCode."]");
		$arr['paymentid'] = 2;
	}
	if($type == 'atm')
	{
		$arr['paymentid'] = 1;
		$url = "atm.html";
		$msg = "Bạn vui lòng thanh toán trực tiếp.";
	}
	
	$id = $_SESSION['id_hotel'];
	
	vaUpdate('orders', $arr, ' id= '.$id);
	$page=$url;
	page_transfer2($page);
}
function Payment()
{
	//$cost = $_GET['cost'];
	//$code = $_GET['code'];
	//$name = $_GET['name'];
	//$phone = $_GET['phone'];
	
	//$page="index.php?do=orders&act=paymentlink&code=".$code."&name=".$name."&cost=".$cost."&phone=".$phone;
	//page_transfer2($page);
}
function POSTVALUE2($arr, $name_get, $name_post)
{
	$value = isset($_POST[$name_post]) ? $_POST[$name_post] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		$arr[$name_get] = $value;	
	}
	
	return $arr;
}

function POSTVALUE($arr, $name)
{
	$value = isset($_POST[$name]) ? $_POST[$name] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		$arr[$name] = $value;	
	}
	
	return $arr;
}
function GETVALUE2($arr, $name_get, $name_post)
{
	$value = isset($_GET[$name_post]) ? $_GET[$name_post] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		$arr[$name_get] = $value;	
	}
	
	return $arr;
}

function GETVALUE($arr, $name)
{
	$value = isset($_GET[$name]) ? $_GET[$name] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		$arr[$name] = $value;	
	}
	
	return $arr;
}
function POSTRETURN($name_post)
{
	$value = isset($_GET[$name_post]) ? $_GET[$name_post] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		return $value;	
	}
	
	return $arr;
}

function GETRETURN($name)
{
	$value = isset($_GET[$name]) ? $_GET[$name] : '';
	
	if($value != '')
	{
		// Kiem tra du lieu
		
		// Lay du lieu
		return $value;	
	}
	
	return '';
}
function GetSum($arr)
{
	// $start = date("Y-m-d");
	// $end = date("Y-m-d");
	$start = $arr['datepickerbegin'];
	$end = $arr['datepickerend'];
	$sodem = 0;
	for($i=0;$i<20;$i++){
		
		  	if(strtotime($end) < strtotime('+'.$i.' day' , strtotime($start))){
				break;				
			} 
			$sodem++;
	}
	$sodem--;
	
	//echo 'extra: '. getExtraBedPrice($arr['id_hotel']);
	//echo 'tren 6 tuoi: '. SafeFormValue('children_over_six');
	//echo 'duoi 6 tuoi: '. SafeFormValue('children_under_six');
	//echo 'cost room: '. SafeFormValue('cost_room');
	//echo 'so dem: '.$sodem;
	$_SESSION['extrabed'] = getExtraBedPrice($arr['id_hotel']);
	$extratotal = getExtraBedPrice($arr['id_hotel'])*(SafeFormValue('children_over_six'));
	$_SESSION['don_gia'] = ($extratotal+ SafeFormValue('cost_room'));
$total	= $arr['num_room']*($extratotal+ SafeFormValue('cost_room'))*$sodem;

	//echo 'Tong: '.$total;
	return $total;
}
function GetTotalNight($arr)
{
	$start = $arr['datepickerbegin'];
	$end = $arr['datepickerend'];
	$sodem = 0;
	for($i=0;$i<20;$i++){
		
		  	if(strtotime($end) < strtotime('+'.$i.' day' , strtotime($start))){
				break;				
			} 
			$sodem++;
	}
	$sodem--;
	return $sodem;	
	}
function getExtraBedPrice($hotelid)
{
	global $db, $sql;
	$sql = "select extrabed_price from hotels where `id`='".$hotelid."'";
	$row = $db ->getRow($sql);
	return $row['extrabed_price'];
}
function ShowList()
{
	global $db, $hotel, $FullUrl, $cat, $room ;
	// index.php?do=orders&act=order&id=1&group=simple
	$id = GETRETURN('id');
	$bed_group = GETRETURN('group');
	
	$sql = "select * from roomtype where id = ". $id;
	//echo $sql;
	$room = $db->getRow($sql);
	
	$sql = "select *, (select name_vn from provinces where id = hotels.province) as name_province, (select id from provinces where id = hotels.id) as id_province from hotels where id = ". $room['hotelid'];
	//echo $sql;
	$hotel = $db->getRow($sql);
	
	//echo $sql;
}
function EditSM()
{	
	global $db, $FullUrl, $lg, $act;
	
	$arr['active'] = 1;
	$arr = POSTVALUE($arr, 'id_room');
	$arr = POSTVALUE($arr, 'id_hotel');
	$_SESSION['id_hotel'] = $arr['id_hotel']; 
	$arr = POSTVALUE($arr, 'loaiphongkm');
	$arr = POSTVALUE($arr, 'name_vn');
	$arr = POSTVALUE($arr, 'star');
	$arr = POSTVALUE($arr, 'address_vn');
	$arr = POSTVALUE($arr, 'name_province');
	$arr = POSTVALUE($arr, 'id_province');
	$arr = POSTVALUE($arr, 'room_name_vn');
	$arr = POSTVALUE($arr, 'name');
	$arr = POSTVALUE($arr, 'address');
	$arr = POSTVALUE($arr, 'phone');
	$arr = POSTVALUE($arr, 'email');
	$arr = POSTVALUE($arr, 'datepickerbegin');
	$arr = POSTVALUE($arr, 'datepickerend');
	$arr = POSTVALUE($arr, 'num_people');
	$arr = POSTVALUE($arr, 'num_room');	
	$arr = POSTVALUE($arr, 'children_under_six');
	$arr = POSTVALUE($arr, 'children_over_six');
	$arr = POSTVALUE($arr, 'note_hotel');
	$arr = POSTVALUE($arr, 'cost_simple_hotel');
	$arr = POSTVALUE($arr, 'cost_double_hotel');
//	$arr = POSTVALUE($arr, 'extrabed_price');
	$arr['num_day'] = GetTotalNight($arr);
	$arr['total'] =  GetSum($arr);
	$arr['don_gia'] = $_SESSION['don_gia'];
	
	global $db;
	$sql = "select * from constant where name = 'money'";
	$money = $db->getRow($sql);
	$arr['total_vn'] =  $arr['total']*$money['value_vn'];
	$_SESSION['ty_gia'] = $money['value_vn'];
	
	$_SESSION['cost'] = 0;
	$_SESSION['code'] = 0;
	if ($act=="addsm")
	{
		$postId = vaInsert('orders',$arr);
		//$msg="Add successfully";		
		
		$_SESSION['id_room'] = $arr['id_room'];
		$_SESSION['id_hotel'] = $arr['id_hotel'];
		$_SESSION['loaiphongkm'] = $arr['loaiphongkm'];
		$_SESSION['type_room'] = $_POST['type_room'];
		$_SESSION['name_vn'] = $arr['name_vn'];
		$_SESSION['star'] = $arr['star'];
		$_SESSION['address_vn'] = $arr['address_vn'];
		$_SESSION['name_province'] = $arr['name_province'];
		$_SESSION['id_province'] = $arr['id_province'];
		$_SESSION['room_name_vn'] = $arr['room_name_vn'];
		$_SESSION['name'] = $arr['name'];
		$_SESSION['address'] = $arr['address'];
		$_SESSION['phone'] = $arr['phone'];
		$_SESSION['email'] = $arr['email'];
		$_SESSION['datepickerbegin'] = $arr['datepickerbegin'];
		$_SESSION['datepickerend'] = $arr['datepickerend'];
		$_SESSION['num_people'] = $arr['num_people'];
		$_SESSION['num_room'] = $arr['num_room'];
		$_SESSION['children_under_six'] = $arr['children_under_six'];
		$_SESSION['children_over_six'] = $arr['children_over_six'];
		$_SESSION['note_hotel'] = $arr['note_hotel'];
		$_SESSION['cost_simple_hotel'] = $arr['cost_simple_hotel'];
		$_SESSION['cost_double_hotel'] = $arr['cost_double_hotel'];
		$_SESSION['num_day'] = $arr['num_day'];		
		$_SESSION['total'] = $arr['total'];
		$_SESSION['cost'] = $arr['total_vn'];
		
		$_SESSION['code'] = $postId;
		
		// Send Mail
		SendMailOrder($arr);
	}
	else
	{
		$id=$_POST['id'];		
		
		vaUpdate('orders',$arr,' id='.$id);	
		//$msg="Edit successfully";
			
		$_SESSION['total'] = $arr['total'];	
		$_SESSION['cost'] = $arr['total_vn'];
		$_SESSION['code'] = $id;
	}	
	
	$_SESSION['mess'] = $msg;
	$page="index.php?do=orders&act=payment";
	page_transfer2($page);
}
function SendMailOrder($arr)
{
	global $FullUrl;
	
	include("./includes/mail_config.php");
	//$fh = fopen("EmailTemplate/Order.html", 'r');
	$fh = fopen("EmailTemplate/Order2.html", 'r');
	$template = fread($fh, filesize("EmailTemplate/Order2.html"));
	fclose($fh);	
	
	$template = str_replace('[ID_ORDERS]', $_SESSION['code'], $template);
	$template = str_replace('[HOTEL_NAME]', $arr['name_vn'], $template);
	$template = str_replace('[HOTEL_STAR]', $arr['star'], $template);
	$template = str_replace('[HOTEL_ADDRESS]', $arr['address_vn'], $template);
	$template = str_replace('[PROVINCE]', $arr['name_province'], $template);
	$template = str_replace('[ROOM_NAME]', $arr['room_name_vn'], $template);
	$template = str_replace('[ROOM_NAME_TYPE]', ($arr['loaiphongkm']=='1' ? 'Phòng Đơn': 'Phòng Đôi'), $template);
	$template = str_replace('[NAME_SEND]', $arr['name'], $template);
	$template = str_replace('[ADDRESS_SEND]', $arr['address'], $template);
	$template = str_replace('[PHONE_SEND]', $arr['phone'], $template);
	$template = str_replace('[EMAIL_SEND]', $arr['email'], $template);
	$template = str_replace('[DATE_BEGIN]', $arr['datepickerbegin'], $template);
	$template = str_replace('[DATE_END]', $arr['datepickerend'], $template);
	$template = str_replace('[NUM_ROOM]', $arr['num_room'], $template);	
	$template = str_replace('[NUM_DAY]', $arr['num_day'], $template);	
	$template = str_replace('[NUM_UNDER_SIX]', $arr['children_under_six'], $template);
	$template = str_replace('[NUM_OVER_SIX]', $arr['children_over_six'], $template);
	$template = str_replace('[TOTAL]', $arr['total'], $template);
	$template = str_replace('[CONTENT]', $arr['note_hotel'], $template);
	$template = str_replace('[DON_GIA]', $arr['don_gia'], $template);
	
	$template = str_replace('[COMMENT]', '<a href="www.timkhachsan.vn" target="_blank">www.timkhachsan.vn</a>', $template);
	
	$mail_subject = 'Đăng Ký Đơn Hàng - Hi, '.$arr['name'].' - HDKS ['.$_SESSION['code'].']';
	
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/bottom.jpg','bottom','bottom.jpg'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/date-bg.jpg','date-bg','date-bg.jpg'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/divider-1.jpg','divider-1','divider-1.jpg'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/divider-2.jpg','divider-2','divider-2.jpg'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/lg.png','lg','lg.png'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/tl-corner.jpg','tl-corner','tl-corner.jpg'); 
	$mail->AddEmbeddedImage('http://www.timkhachsan.vn/images/EmailTemplate/wl.png','wl','wl.png'); 
	
	$mail_content = $template;
	$mail->Subject = $mail_subject;
	$mail->MsgHTML($mail_content);
	$mail->FromName = '[Tìm Khách Sạn]';
	//$mail_to = CST_ADMIN_EMAIL;
	//$mail->AddAddress($mail_to, "Webmaster");
	$mail->AddAddress($arr['email'], $arr['name']);
	
	
	
	//$mail->AddBCC('kamnhattuan@gmail.com', 'Kam');
	$mail->Send();	
}
function Favourite()
{
	if(isset($_SESSION['member_id']))
	{
		global $db,$hotels,$page,$plpage,$set_per_page,$c,$cat,$title_bar,$lang, $idRoot, $idMenuMain, $index_on;
		global $cat1, $cat2, $lg, $prefix_url, $title_page, $FullUrl;
		
		$title_bar = FAVOURITE_LIST;
		
		$cat = $cat2;
		$title_page = $cat2["title_$lg"];
		
		$set_per_page = CST_PRODUCT_PAGING_LIST;
		$sql="select id, name_vn, name_en, size, price, hotelion_vn, hotelion_en, unique_key_vn, unique_key_en, img, cid, seo_f_vn, seo_f_en, view, vote from favourite f, hotels p where f.fav_pro_id = p.id and fav_mem_id = ".$_SESSION['member_id']." order by p.id desc ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage_seo($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);	
		$hotels = $db->getAll($sqlstmt);
	}
}

function Search()
{	
	global $db,$hotels,$page,$plpage,$set_per_page,$c,$cat,$title_bar,$lang, $idRoot, $index_on;
	
	$key = CleanSQLInjection(trim(isset($_GET['key'])?$_GET['key']:''));
	
	if(!empty($key))
	{
		$title_bar =  RESULT_SEARCH;
	
		$set_per_page = CST_PRODUCT_PAGING_SEARCH; 
		
		$sql = "select * from hotels where name_vn like '%".$key."%' or name_en like '%".$key."%' or code like '%".$key."%' or descs_vn like '%".$key."%' or descs_en like '%".$key."%' ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);	
		$hotels = $db->getAll($sqlstmt);
	}
}

function ShowDetail($id)
{
	global $db, $hotel, $hotels, $cat, $title_bar, $anothers, $idRoot, $idMenuMain, $thumbs, $hotel, $hotels_anews, $imgs_hotel, $rooms_hotel;
	global $cat1, $cat2, $lg, $prefix_url, $title_page, $keywords, $descriptions, $sizes, $FullUrl, $page, $plpagecomment,$set_per_page, $hotel_comments;
	
	if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
	}else{
		$cat = $cat1;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
	}
	
	$hotel_key = CleanSQLInjection(trim(isset($_GET['unique_key'])?$_GET['unique_key']:''));
	$sql = "select * from hotels where unique_key_$lg='$hotel_key' and cid=" . $cat['id'];
	
	$hotel = $hotel = $db->getRow($sql);
	$hotel_id = $hotel['id'];
	
	CountHotelView($hotel_id, $db);
	//seo
	$title_page = $hotel["title_$lg"];
	$keywords = $hotel["keyword_$lg"];
	$descriptions = $hotel["des_$lg"];
	
	$sql="select * from hotels where active=1 and cid=".$hotel['cid']." and id<>$hotel_id order by num asc, view desc, id desc limit 0,10";
	$hotels_anews = $db->getAll($sql);
		
	$set_per_page = CST_PRODUCT_PAGING_COMMENT;
	$sql = "select cmt_content, cmt_insert_date, (select name from member where id = cmt_mem_id) as mem_name from comments where cmt_pro_id = ".$hotel['id']." and cmt_active = 1 order by cmt_id desc";
	
	$plpagecomment = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$hotel_comments = $db->getAll($sqlstmt);
	
	$sql = "select * from imgslider where hotelid = ". $hotel_id;
	$imgs_hotel = $db->getAll($sql);
	
	$sql = "select * from roomtype where active=1 and hotelid=".$hotel_id;
	$rooms_hotel = $db->getAll($sql);
}

function CountHotelView($hotelId, $db){
	$UniqueSession = "hotelsAreViewed";
	$hotelIdString = "[" . $hotelId . "]";
	
	if(!isset($_SESSION[$UniqueSession]))
	{
		$_SESSION[$UniqueSession] = $hotelIdString;
		
		$sql = "update hotels set view = view + 1 where id = $hotelId";	
		$db->query($sql);
	}
	else
	{
		$mystring = $_SESSION[$UniqueSession];
		$findme = $hotelIdString;
		$pos = strpos($mystring, $findme);
		
		if($pos === false)
		{
			$_SESSION[$UniqueSession] .= $hotelIdString;
			
			$sql = "update hotels set view = view + 1 where id = $hotelId";	
			$db->query($sql);
		}
	}
}

?>